package chen.yi.component.mysql.utils;

import chen.yi.component.mysql.vo.system.*;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.json.JSONUtil;

import java.util.Objects;

/**
 * 数据缓存
 *
 * @author huat
 **/
public class TableAttrUtils {
    private static FieldTypeVO FIELD_TYPE;
    private static IndexTypeVO INDEX_TYPE;
    private static ForeignKeyVO FOREIGN_KEY ;
    private static VirtualTypeVO VIRTUAL_TYPE;
    private static TableEngineVO ENGINE_TYPE;

    /**
     * 获取字段列数据类型
     * @return
     */
    public static FieldTypeVO getFieldType() {
        if (Objects.isNull(FIELD_TYPE)) {
            synchronized (FieldTypeVO.class){
                if(Objects.isNull(FIELD_TYPE)){
                    String fieldJson = ResourceUtil.readUtf8Str("json/fieldType.json");
                    FIELD_TYPE = JSONUtil.toBean(fieldJson, FieldTypeVO.class);
                    return FIELD_TYPE;
                }
            }
        }
        return FIELD_TYPE;
    }
    /**
     * 获取索引
     * @return
     */
    public static IndexTypeVO getIndexType() {
        if (Objects.isNull(INDEX_TYPE)) {
            synchronized (IndexTypeVO.class){
                if(Objects.isNull(INDEX_TYPE)){
                    String fieldJson = ResourceUtil.readUtf8Str("json/indexType.json");
                    INDEX_TYPE = JSONUtil.toBean(fieldJson, IndexTypeVO.class);
                    return INDEX_TYPE;
                }
            }
        }
        return INDEX_TYPE;
    }
    /**
     * 获取外键约束
     * @return
     */
    public static ForeignKeyVO getForeignKey() {
        if (Objects.isNull(FOREIGN_KEY)) {
            synchronized (ForeignKeyVO.class){
                if(Objects.isNull(FOREIGN_KEY)){
                    String fieldJson = ResourceUtil.readUtf8Str("json/foreignKey.json");
                    FOREIGN_KEY = JSONUtil.toBean(fieldJson, ForeignKeyVO.class);
                    return FOREIGN_KEY;
                }
            }
        }
        return FOREIGN_KEY;
    }
    /**
     * 获取虚拟列
     * @return
     */
    public static VirtualTypeVO getVirtualType() {
        if (Objects.isNull(VIRTUAL_TYPE)) {
            synchronized (VirtualTypeVO.class){
                if(Objects.isNull(VIRTUAL_TYPE)){
                    String fieldJson = ResourceUtil.readUtf8Str("json/virtualType.json");
                    VIRTUAL_TYPE = JSONUtil.toBean(fieldJson, VirtualTypeVO.class);
                    return VIRTUAL_TYPE;
                }
            }
        }
        return VIRTUAL_TYPE;
    }
    /**
     * 获取表引擎类型
     * @return
     */
    public static TableEngineVO getTableEngine() {
        if (Objects.isNull(ENGINE_TYPE)) {
            synchronized (VirtualTypeVO.class){
                if(Objects.isNull(ENGINE_TYPE)){
                    String fieldJson = ResourceUtil.readUtf8Str("json/tableEngine.json");
                    ENGINE_TYPE = JSONUtil.toBean(fieldJson, TableEngineVO.class);
                    return ENGINE_TYPE;
                }
            }
        }
        return ENGINE_TYPE;
    }
}
